package com.xb.cloud.usercenter.client;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.usercenter.client.vo.FavoriteSkuVo;
import com.xb.cloud.usercenter.service.UserFavoriteProductSkuService;

/**
 * 
 * @ClassName: UserFavoriteProductSkuAdapter
 * @Description: 买家用户productSku收藏管理
 * @author: 鲜备科技.韦现法
 * @date: 2018年2月1日 上午午9:17:00
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class UserFavoriteProductSkuAdapter extends Adapter {

	@Resource
	UserFavoriteProductSkuService userFavoriteProductSkuService;

	private final Logger logger = LoggerFactory.getLogger(UserFavoriteShopAdapter.class);

	@Override
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		
		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		long currentUserId = context.getCurrentUserId();
		Long skuId=null;
		FavoriteSkuVo favoriteSkuVo=new FavoriteSkuVo();
		favoriteSkuVo.setCompanyId(companyId);
		favoriteSkuVo.setEndUserId(currentUserId);
		
		if("/h5/favoriteSku/add".equalsIgnoreCase(urlPath)){
			try{
				skuId = Long.valueOf(bizInfo.get("skuId"));
			}catch(NumberFormatException e){
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			favoriteSkuVo.setProductSkuId(skuId);
			//添加收藏
			rtnInfo=userFavoriteProductSkuService.addFavoriteSku(favoriteSkuVo);
		}else if("/h5/favoriteSku/cancel".equalsIgnoreCase(urlPath)){
			try{
				skuId = Long.valueOf(bizInfo.get("skuId"));
			}catch(NumberFormatException e){
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			favoriteSkuVo.setProductSkuId(skuId);
			//取消收藏
			rtnInfo=userFavoriteProductSkuService.cancelFavoriteSku(favoriteSkuVo);
		}else if("/h5/favoriteSku/isFollowed".equalsIgnoreCase(urlPath)){
			try{
				skuId = Long.valueOf(bizInfo.get("skuId"));
			}catch(NumberFormatException e){
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			favoriteSkuVo.setProductSkuId(skuId);
			//判断是否收藏
			rtnInfo=userFavoriteProductSkuService.isFollowed(favoriteSkuVo);
		}else if("/h5/favoriteSku/list".equalsIgnoreCase(urlPath)){
			//收藏列表
			Integer pageNum=null;
			Integer pageSize=null;
			try{
				pageNum=Integer.parseInt(bizInfo.get("pageNum"));
				pageSize=Integer.parseInt(bizInfo.get("pageSize"));
			}catch(NumberFormatException e){
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			
			favoriteSkuVo.setPageNum(pageNum);
			favoriteSkuVo.setPageSize(pageSize);
			rtnInfo=userFavoriteProductSkuService.getFavoriteShopList(favoriteSkuVo);
		}
		
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/favoriteSku/**";
	}

	@Override
    public boolean isCheckUserToken (){
        return true;
    } 
}
